package cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node;

import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.task.NodeExecResult;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.RunStatusEnum;
import cn.hutool.core.lang.UUID;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author pucong
 * @description 执行node信息
 * @create 2024-09-24
 */
@Data
@NoArgsConstructor
@Accessors(chain = true)
public class ExecNode implements Serializable {

    private static final long serialVersionUID = 1L;

    private String uuid;

    private String instanceNodeId;

    private String nodeName;
    /**
     * 节点标签
     */
    private String nodeTag;

    private String nodeType;

    /**
     * 节点脚本
     */
    private String nodeScript;

    /**
     * 是否忽略、跳过执行
     */
    private volatile Boolean isIgnore;

    private List<ExecParamIn> paramInList;

    private List<ExecParamOut> paramOutList;

    private volatile NodeExecResult nodeExecResult;

    public ExecNode(TaskNode taskNode) {
        this.uuid = taskNode.getUuid();
        this.instanceNodeId = UUID.fastUUID().toString();
        this.nodeName = taskNode.getNodeName();
        this.nodeTag = taskNode.getNodeTag();
        this.nodeType = taskNode.getNodeType();
        this.nodeScript = taskNode.getNodeScript();
        this.nodeExecResult = NodeExecResult.ready();
        if (taskNode.getTaskNodeParamInList() != null) {
            List<TaskNodeParamIn> taskNodeParamInList1 = taskNode.getTaskNodeParamInList();
            List<ExecParamIn> results = new ArrayList<>();
            for (TaskNodeParamIn taskNodeParamIn : taskNodeParamInList1) {
                results.add(new ExecParamIn(taskNodeParamIn));
            }
            this.paramInList = results;
        }
        if (taskNode.getTaskNodeParamOutList() != null) {
            List<TaskNodeParamOut> taskNodeParamOuts = taskNode.getTaskNodeParamOutList();
            List<ExecParamOut> results = new ArrayList<>();
            for (TaskNodeParamOut taskNodeParamOut : taskNodeParamOuts) {
                results.add(new ExecParamOut(taskNodeParamOut));
            }
            this.paramOutList = results;
        }
    }

    public Object getParamOutValue(String paramCode) {
        ExecParamOut execParamOut = paramOutList.stream().filter(paramOut -> paramCode.equals(paramOut.getParamCode())).findFirst().orElse(null);
        if (execParamOut != null) {
            return execParamOut.getDateValue();
        }
        return null;
    }

    public Object getParamInValue(String paramCode) {
        ExecParamIn execParamIn = paramInList.stream().filter(paramOut -> paramCode.equals(paramOut.getParamCode())).findFirst().orElse(null);
        if (execParamIn != null) {
            return execParamIn.getDateValue();
        }
        return null;
    }

    public void setNodeExecResultIgnore() {
        nodeExecResult.setRunStatus(RunStatusEnum.已跳过);
    }

    public void setNodeExecResultOk() {
        nodeExecResult.setRunStatus(RunStatusEnum.执行成功);
    }

    public void setNodeExecResultError(String logContent) {
        nodeExecResult.setErrorLog(logContent);
        nodeExecResult.setRunStatus(RunStatusEnum.执行成功);
    }
}
